package agreement

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"
	"gitee.com/liuxuezhan/ar-platform/admin-api/pkg/code"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/setting/client/agreement"
	"github.com/zeromicro/go-zero/core/logc"
	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateAgreementLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// NewUpdateAgreementLogic 新增或修改协议
func NewUpdateAgreementLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateAgreementLogic {
	return &UpdateAgreementLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UpdateAgreementLogic) UpdateAgreement(req *types.AgreementReq) (resp *types.Empty, err error) {

	if req.Type != 1 && req.Type != 2 {
		return nil, code.ParamsInvalid
	}

	u, err := l.svcCtx.SettingService.AgreementRpc.UpdateAgreement(l.ctx, &agreement.AddAgreementReq{MerchantId: 1, Type: req.Type, Content: req.Content})

	if err != nil {
		logc.Error(l.ctx, "UpdateAgreement error:", err)
		return nil, err
	}

	if u.Code != int64(code.SettingRpcSuccessful.Code()) {
		return nil, code.BusinessFailure
	}

	resp = &types.Empty{}
	return
}
